/**
 * 
 */
package com.ssm.util.security;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ssm.model.Model;
import com.ssm.model.bean.SsmGroups;
import com.ssm.model.bean.SsmUserGroup;
import com.ssm.model.bean.SsmUsers;
import com.ssm.model.mapper.GroupsMapper;
import com.ssm.model.mapper.UserGroupMapper;
import com.ssm.model.mapper.UserMapper;

/**
 * @author WTC E-mail:546895488@qq.com 
 * @version 创建时间：2016年2月7日 下午3:23:33 
 */
@Component
public class UserRoleInfo {
	
	@Autowired
	Model M;
	List<SsmUserGroup> list1;
	
	String username;
	

	
	public List<String> getRolesName(){
		//SqlSession sqlSession = MybatisHelper.getSqlSession();
		
		List<String> list =new ArrayList<String>();
		UserMapper um=M.getMapper(UserMapper.class);
        SsmUsers user=new SsmUsers();
        user.setUsername(this.username);
        user=um.selectOne(user);
        //获取用户id
        //System.out.println("userid:"+user.getId());
        SsmUserGroup sug=new SsmUserGroup();
        sug=new SsmUserGroup();
        sug.setUserid(user.getId());
        UserGroupMapper ugm=M.getMapper(UserGroupMapper.class);
        //通过用户id获取用户组
        System.out.println("userid:"+sug.getUserid());
        
        list1=ugm.select(sug);
        //sug=ugm.selectOne(sug);  
        
        for(SsmUserGroup ug:list1)
        {
	        GroupsMapper gm=M.getMapper(GroupsMapper.class);
	        SsmGroups sg=new SsmGroups();
	        //通过用户组id获取所属角色
	        System.out.println(ug==null);
	        System.out.println("groupid:"+ug.getGroupid());
	        sg.setId(ug.getGroupid());
	        
	        sg=gm.selectOne(sg);
	        
	        //获取角色名
	        String name=sg.getName();
	        
			list.add(name);
        }
        
		return list;
		
	}
	
	
	public List<String> getPermissionsName(){
		//SqlSession sqlSession = MybatisHelper.getSqlSession();
		List<String> list =new ArrayList<String>();
		
		for(SsmUserGroup ug:list1)
		{
			SsmGroups sg=new SsmGroups();
			GroupsMapper urm=M.getMapper(GroupsMapper.class);
			sg=urm.selectByPrimaryKey(ug.getGroupid());
			String rules=sg.getRule();
			String[] datarules =rules.split("%"); 
			for(int i=0;i<datarules.length;i++){
				list.add(datarules[i]);
			}
		}
		
		return list;
	}

	/**
	 * @param username2
	 */
	public void setUsername(String username2) {
		// TODO Auto-generated method stub
		//@author WTC E-mail:546895488@qq.com 
		//@version 创建时间：2016年2月11日 下午1:44:06 
		username=username2;
	}

}
